//
//  ZBWatermarkTool.m
//  ZBUitilty
//
//  Created by xzb on 2018/8/22.
//

#import "ZBWatermarkTool.h"

@implementation ZBWatermarkTool
+ (UIImage *)watermarkImage:(UIImage *
                             )image withLogo:(UIImage *)logoImage text:(NSString *)text
{
    if (!image) {
        return nil;
    }
    
    UIGraphicsBeginImageContext(image.size);
    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    
    CGFloat scale = image.size.width / 375.f;
    
    UIFont *font = [UIFont systemFontOfSize:scale * 12];
    if (@zb_available(iOS 8.2, *)) {
        font = [UIFont systemFontOfSize:scale * 12 weight:UIFontWeightLight];
    }
    //添加文字
    NSDictionary *attributes = @{NSFontAttributeName:font,
                                 NSForegroundColorAttributeName:[UIColor colorWithHexString:@"0xffffff"]};
    CGSize textSize = [text sizeWithAttributes:attributes];
    CGRect textRect = CGRectMake(image.size.width - textSize.width - 8 * scale,
                                 image.size.height - textSize.height - 8 * scale,
                                 textSize.width,
                                 textSize.height);
    [text drawInRect:textRect withAttributes:attributes];
    
    //添加logo水印
    CGSize logoSize = CGSizeMake(43.5, 12);
    [logoImage drawInRect:CGRectMake(image.size.width - logoSize.width * scale - 8 * scale,
                                     textRect.origin.y - logoSize.height * scale - 2 * scale,
                                     logoSize.width * scale,
                                     logoSize.height * scale)];
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return result;
}
@end
